using System;
using System.Collections.Generic;
using UMC.Web;


[assembly: Apiumc]
namespace UMC.WorkFlow
{
    [Apiumc("WorkFlow", "Shape", Auth = WebAuthType.Admin, Desc = "工作流设计")]
    public class DesignShapeActivity : UMC.Web.WebActivity
    {
        TaskInstance taskInstance;




        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var key = this.AsyncDialog("Key", g => this.DialogValue("Promotion"));
            var Task = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Task", "none"));


            if (Task.HasValue)
            {
                this.taskInstance = UMC.Data.JSON.Deserialize<TaskInstance>(TaskManager.Design(Task.Value));
                var kId = this.AsyncDialog("Shape", g =>
                {
                    var ts = new List<Shape>();
                    switch (key)
                    {
                        case "FromId":
                        case "ToId":
                            break;
                        default:
                            ts.Add(new UMC.WorkFlow.Subassembly.Shape() { Id = "ALL", Text = "所有节点" });
                            break;
                    }
                    ts.AddRange(this.taskInstance.Shapes);

                    var gl = UIDialog.Create(new UIGridDialog.Header("Id", 0).PutField("Text", "名称").PutField("Id", "标志"), ts.ToArray());
                    gl.Title = "审批结点";
                    gl.CloseEvent = "UI.Event";
                    return gl;
                });
                if ("ALL".Equals(kId, StringComparison.CurrentCultureIgnoreCase))
                {

                    this.Context.UIEvent(key, new ListItem("所有节点", "ALL")), true);
                }
                else
                {
                    var shape = this.taskInstance.Shapes.Find(g => g.Id == kId);
                    this.Context.UIEvent(key, new ListItem(shape.Text + ":" + shape.Id, shape.Id)), true);
                }
            }
            else
            {
                // var proms = UMC.Data.Reflection.Configuration("database");

                this.Context.UIEvent(key, new ListItem(this.AsyncDialog("database", g =>
                {
                    var proms = UMC.Data.Reflection.Configuration("database").Providers.GetEnumerator();
                    var select = new List<WebMeta>();
                    while (proms.MoveNext())
                    {
                        select.Add(new WebMeta().Put("name", proms.Current.Name));

                    }
                    
                    var gl = UIDialog.Create(new UIGridDialog.Header("name", 0).PutField("name", "数据库节点"), select.ToArray());
                    gl.Title = "数据库节点";
                    gl.CloseEvent = "UI.Event";
                    return gl;
                }))), true);

            }
        }

    }
}